//
//  SettleManageTypeVM.swift
//  kxf
//
//  Created by Elavatine on 2024/10/13.
//

import Foundation
import UIKit
import SwiftUI

class SettleManageTypeVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+kFitWidth(50)*2
    
    var btnArray:[UIButton] = [UIButton]()
    var tapBlock:((NSDictionary)->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var photoTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "结算周期"
        return vm
    }()
    
    lazy var settleTypeArray: NSArray = {
        return [["type":"0","name":"D0结算"],
                ["type":"1","name":"D1结算"],
                ["type":"2","name":"T0结算"],
                ["type":"3","name":"T1结算"],
                ["type":"4","name":"定时结算"]]
    }()
}

extension SettleManageTypeVM{
    @objc func btnTapAction(btnSender:UIButton){
        for button in btnArray{
            button.isSelected = false
        }
        btnSender.isSelected = true
        
        let tapIndex = btnSender.tag - 4040
        let dict = settleTypeArray[tapIndex]as? NSDictionary ?? [:]
        if self.tapBlock != nil{
            self.tapBlock!(dict)
        }
    }
}


extension SettleManageTypeVM{
    func initUI() {
        addSubview(photoTitleVm)
        initMsg()
    }
    func initMsg() {
        let btnWidth = (SCREEN_WIDHT - kFitWidth(16)*2 - kFitWidth(12)*2)/3
        let btnHeight = kFitWidth(44)
        
        for i in 0..<settleTypeArray.count{
            let dict = settleTypeArray[i]as? NSDictionary ?? [:]
            
            let btn = UIButton()
            let originX = kFitWidth(16) + (btnWidth + kFitWidth(12))*CGFloat(i%3)
            let originY = kFitWidth(3) + (kFitWidth(50) + kFitWidth(3)) * CGFloat(i/3) + photoTitleVm.selfHeight
            btn.frame = CGRect.init(x: originX, y: originY, width: btnWidth, height: btnHeight)
            btn.backgroundColor = .COLOR_LIGHT_GREY
            btn.setTitle("\(dict["name"]as? String ?? "")", for: .normal)
            btn.setTitleColor(.COLOR_TEXT_BLACK999, for: .normal)
            btn.setTitleColor(.white, for: .selected)
            btn.setBackgroundImage(createImageWithColor(color: .THEME), for: .selected)
            btn.layer.cornerRadius = kFitWidth(4)
            btn.clipsToBounds = true
            btn.tag = 4040 + i
            btn.addTarget(self, action: #selector(btnTapAction(btnSender: )), for: .touchUpInside)
            
            addSubview(btn)
            btnArray.append(btn)
            
            if dict["type"]as? String ?? "" == UserModel.shared.settleCycle{
                btn.isSelected = true
                if self.tapBlock != nil{
                    self.tapBlock!(dict)
                }
            }
        }
    }
}
